package com.jmxcfc.blfsc.common.exception;


import com.jmxcfc.blfsc.common.common.enums.BaseErrorEnum;
import com.jmxcfc.blfsc.common.common.enums.ProcessStateEnum;
import com.jmxcfc.blfsc.common.common.response.BaseResponse;
import com.jmxcfc.fs.common.ErrorEnum;
import com.jmxcfc.fs.common.handler.GeneralExceptionHandler;
import com.jmxcfc.fs.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Controller 异常统一全局处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler extends GeneralExceptionHandler {

    /**
     * 交易初始化异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(InitResponseException.class)
    public <T> Result<BaseResponse<T>> initResponseExceptionHandle(InitResponseException e) {
        log.error(e.getMessage(), e);
        return Result.fail(ErrorEnum.FAIL, BaseResponse.fail(ProcessStateEnum.FAILURE, BaseErrorEnum.EXCEPTION_INIT_RESPONSE, e));
    }

    /**
     * 交易头部验证异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ValidHeaderException.class)
    public <T> Result<BaseResponse<T>> runtimeExceptionHandle(ValidHeaderException e) {
        log.error(e.getMessage(), e);
        return Result.fail(ErrorEnum.FAIL, BaseResponse.fail(ProcessStateEnum.FAILURE, BaseErrorEnum.EXCEPTION_VALID_HEADER, e));
    }

    /**
     * 交易头部验证异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ValidBodyException.class)
    public <T> Result<BaseResponse<T>> runtimeExceptionHandle(ValidBodyException e) {
        log.error(e.getMessage(), e);
        return Result.fail(ErrorEnum.FAIL, BaseResponse.fail(ProcessStateEnum.FAILURE, BaseErrorEnum.EXCEPTION_VALID_BODY, e));
    }

    /**
     * BeforeOutboundException异常统一处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BeforeOutboundException.class)
    public <T> Result<BaseResponse<T>> beforeOutboundException(BeforeOutboundException e) {
        log.error(e.getMessage(), e);
        return Result.fail(ErrorEnum.FAIL, BaseResponse.fail(ProcessStateEnum.FAILURE, BaseErrorEnum.EXCEPTION_BEFORE_PROCESS, e));
    }


    /**
     * OutboundException异常统一处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(OutboundException.class)
    public <T> Result<BaseResponse<T>> exceptionHandle(OutboundException e) {
        log.error(e.getMessage(), e);
        return Result.fail(ErrorEnum.FAIL, BaseResponse.fail(ProcessStateEnum.EXCEPTION, BaseErrorEnum.EXCEPTION_OUTBOUND, e));
    }

    /**
     * AfterOutboundException异常统一处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(AfterOutboundException.class)
    public <T> Result<BaseResponse<T>> afterOutboundException(AfterOutboundException e) {
        log.error(e.getMessage(), e);
        return Result.fail(ErrorEnum.FAIL, BaseResponse.fail(ProcessStateEnum.EXCEPTION, BaseErrorEnum.EXCEPTION_AFTER_PROCESS, e));
    }

}
